﻿using System;
using System.Collections.Generic;
using System.Data;
using FoundationClass.Entity;

namespace FoundationClass.Data
{
    public static class PaymentData
    {
        internal static Payment GetPayment(int paidId)
        {
            Payment payment = null;
            DataTable paymentDt = DatabaseManager.Read("Payment");
            foreach (DataRow row in paymentDt.Rows)
            {
                if ((row["id"]).Equals(paidId.ToString()))
                {
                    payment = new Payment
                    {
                        PaidId = int.Parse(row["id"].ToString()),
                        PaidTime = string.IsNullOrEmpty(row["paidtime"] as string) ? DateTime.Now : DateTime.Parse(row["paidtime"].ToString()),
                        TotalAmount = double.Parse(row["totalamount"].ToString()),
                        Employee = UserData.GetUser((string)row["userid"])
                    };
                    break;
                }
            }
            return payment;
        }

        internal static void InsertPayment(Payment payment)
        {
            DataTable paymentDt = DatabaseManager.Read("Payment");
            int newId = GetMaxPaymentId() + 1;
            payment.PaidId = newId;
            paymentDt.Rows.Add(newId, payment.PaidTime, payment.TotalAmount, payment.Employee.Id);
            DatabaseManager.Write(paymentDt);
        }

        public static void UpdatePayment(Payment payment)
        {
            DataTable paymentDt = DatabaseManager.Read("Payment");
            foreach (DataRow row in paymentDt.Rows)
            {
                if (row["id"].Equals(payment.PaidId.ToString()))
                {
                    row["paidtime"] = payment.PaidTime;
                   
                    break;
                }
            }
            DatabaseManager.Write(paymentDt);
        }

        private static int GetMaxPaymentId()
        {
            DataTable paymentDt = DatabaseManager.Read("Payment");
            int rowcount = paymentDt.Rows.Count;
            if (rowcount == 0) 
                return 1;
            return int.Parse(paymentDt.Rows[rowcount - 1]["id"].ToString());
        }

        internal static IList<Payment> GetAllPayments()
        {
            try
            {
                List<Payment> payments = null;
                DataTable paymentDt = DatabaseManager.Read("Payment");
                if (paymentDt.Rows.Count > 0)
                {
                    payments = new List<Payment>();
                    foreach (DataRow row in paymentDt.Rows)
                    {
                        payments.Add(new Payment
                        {
                            PaidId = int.Parse(row["id"].ToString()),
                            PaidTime = string.IsNullOrEmpty(row["paidtime"] as string) ? DateTime.Now : DateTime.Parse(row["paidtime"].ToString()),
                            TotalAmount = double.Parse(row["totalamount"].ToString()),
                            Employee = UserData.GetUser((string)row["userid"])
                        });
                    }

                }
                return payments;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
